package com.wenx.concurrent.pk;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;

public class ThreadPkTest2 {


    public static void main(String[] args) throws InterruptedException {

        final List<Integer> list = new ArrayList<>();

        Instant start = Instant.now();

        CountDownLatch countDownLatch = new CountDownLatch(10000);

        Random random = new Random();
        for (int i = 0; i < 10000; i++) {
            Thread thread = new Thread(() -> {
                list.add(random.nextInt());
                countDownLatch.countDown();
            });
            thread.start();
        }
        //等待10000个线程同时向list插入数据，线程不安全，list.size()小于10000
        countDownLatch.await();
        Instant end = Instant.now();
        System.out.println("times = " + Duration.between(start,end).toMillis());
        System.out.println(list.size());
    }

}
